package com.ruoyi.bantong.controller;

import java.util.List;

import com.ruoyi.bantong.mapper.BtOrganizationMapper;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bantong.domain.BtOrganization;
import com.ruoyi.bantong.service.IBtOrganizationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 机构Controller
 * 
 * @author laohei
 * @date 2021-01-27
 */
@RestController
@RequestMapping("/bantong/organization")
public class BtOrganizationController extends BaseController
{
    @Autowired
    private IBtOrganizationService btOrganizationService;
    @Autowired
    private BtOrganizationMapper btOrganizationMapper;

    /**
     * 查询机构列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:organization:list')")
    @GetMapping("/list")
    public TableDataInfo list(BtOrganization btOrganization)
    {
        startPage();
        List<BtOrganization> list = btOrganizationService.selectBtOrganizationList(btOrganization);
        return getDataTable(list);
    }

    /**
     * 导出机构列表
     */
    //@PreAuthorize("@ss.hasPermi('bantong:organization:export')")
    @Log(title = "机构", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BtOrganization btOrganization)
    {
        List<BtOrganization> list = btOrganizationService.selectBtOrganizationList(btOrganization);
        ExcelUtil<BtOrganization> util = new ExcelUtil<BtOrganization>(BtOrganization.class);
        return util.exportExcel(list, "organization");
    }

    /**
     * 获取机构详细信息
     */
    //@PreAuthorize("@ss.hasPermi('bantong:organization:query')")
    @GetMapping(value = "/{orgId}")
    public AjaxResult getInfo(@PathVariable("orgId") Integer orgId)
    {
        return AjaxResult.success(btOrganizationService.selectBtOrganizationById(orgId));
    }

    /**
     * 新增机构
     */
    //@PreAuthorize("@ss.hasPermi('bantong:organization:add')")
    @Log(title = "机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BtOrganization btOrganization)
    {
        String phone = btOrganization.getPhone();
        BtOrganization orgTemp = btOrganizationMapper.selectBtOrganizationByPhone(phone,null);
        if(!StringUtils.isNull(orgTemp)){
            return AjaxResult.error("新增机构'" + phone + "'失败，手机号码已存在");
        }
        return toAjax(btOrganizationService.insertBtOrganization(btOrganization));
    }

    /**
     * 修改机构
     */
    //@PreAuthorize("@ss.hasPermi('bantong:organization:edit')")
    @Log(title = "机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BtOrganization btOrganization)
    {
        String phone = btOrganization.getPhone();
        if(!StringUtils.isEmpty(phone)){
            BtOrganization orgTemp = btOrganizationMapper.selectBtOrganizationByPhone(phone,btOrganization.getOrgId());
            if(!StringUtils.isNull(orgTemp)){
                return AjaxResult.error("修改机构'" + phone + "'失败，手机号码已存在");
            }
        }
        return toAjax(btOrganizationService.updateBtOrganization(btOrganization));
    }

    /**
     * 删除机构
     */
    //@PreAuthorize("@ss.hasPermi('bantong:organization:remove')")
    @Log(title = "机构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orgIds}")
    public AjaxResult remove(@PathVariable Integer[] orgIds)
    {
        return toAjax(btOrganizationService.deleteBtOrganizationByIds(orgIds));
    }


    /**
     * 充值
     */
    //@PreAuthorize("@ss.hasPermi('bantong:user:edit')")
    @Log(title = "机构", businessType = BusinessType.UPDATE)
    @PutMapping("/recharge")
    public AjaxResult recharge(@RequestBody BtOrganization org)
    {
        int rows = 0;
        try {
            rows = btOrganizationService.recharge(org);
            return rows > 0 ? AjaxResult.success() : AjaxResult.error();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof CustomException){
                return AjaxResult.error(e.getMessage());
            }else {
                return AjaxResult.error();
            }
        }
    }
}
